﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace VanWest.Web.controls
{
    public partial class FeeCalculator : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitDDLs();
            }
        }

        private void InitDDLs()
        {
            // Initiate Study Schedule
            _ddlStudySchedule.Items.Add(new ListItem("Select Program", "0"));
            _ddlStudySchedule.Items.Add(new ListItem("Full-time Intensive (27 hours/week)", "27"));
            _ddlStudySchedule.Items.Add(new ListItem("Full-time essential (21 hours/week)", "21"));
            _ddlStudySchedule.Items.Add(new ListItem("Part-time AM (15 hours/week)", "15"));
            _ddlStudySchedule.Items.Add(new ListItem("Part-time PM (12 hours/week)", "12"));
            _ddlStudySchedule.Items.Add(new ListItem("CEB (30 hours/week)", "30"));

            // Initiate Number of weeks
            ListItem item = new ListItem("Select Weeks", "0");
            _ddlNumOfWeeks.Items.Add(item);
            for (int i = 4; i <= 52; i++)
            {
                item = new ListItem();
                item.Text = i + " weeks";
                item.Value = i.ToString();
                _ddlNumOfWeeks.Items.Add(item);
            }

            // Initiate Accommodation Type
            _ddlAccommodation.Items.Add(new ListItem("None", "0"));
            _ddlAccommodation.Items.Add(new ListItem("Homestay Vancouver (above 16 years)", "840"));
            _ddlAccommodation.Items.Add(new ListItem("Homestay Kelowna (above 16 years)", "700"));
            _ddlAccommodation.Items.Add(new ListItem("Homestay Vancouver (16 years & under)", "868"));
            _ddlAccommodation.Items.Add(new ListItem("Homestay Kelowna (16 years & under)", "868.0"));


            // Initiate airport pick-up
            _ddlAirportTransfer.Items.Add(new ListItem("None", "0"));
            _ddlAirportTransfer.Items.Add(new ListItem("One way (Vancouver)", "80"));
            _ddlAirportTransfer.Items.Add(new ListItem("One way (Kelowna)", "60"));
            _ddlAirportTransfer.Items.Add(new ListItem("Two way (Vancouver)", "160"));
            _ddlAirportTransfer.Items.Add(new ListItem("Two way (Kelowna)", "120"));

            DataBind();
        }

        protected void ddlStudySchedule_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            if (ddl.SelectedValue.Equals("0"))
            {
                _lbStudyScheduleError.Visible = true;
            }
            else
            {
                _lbStudyScheduleError.Visible = false;
                CalculateFees();
            }
        }

        protected void ddlAccommodation_SelectedIndexChanged(object sender, EventArgs e)
        {
            CalculateFees();
        }

        protected void ddlNumOfWeeks_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            if (ddl.SelectedValue.Equals("0"))
            {
                _lbNumOfWeeksError.Visible = true;
            }
            else
            {
                _lbNumOfWeeksError.Visible = false;
                CalculateFees();
            }
        }

        protected void ddlAirportTransfer_SelectedIndexChanged(object sender, EventArgs e)
        {
            CalculateFees();
        }

        private void CalculateFees()
        {
            if (!_ddlStudySchedule.SelectedValue.Equals("0") && !_ddlNumOfWeeks.SelectedValue.Equals("0"))
            {
                double materialFees = 0;
                int studySchedule = int.Parse(_ddlStudySchedule.SelectedValue);
                int numOfWeeks = int.Parse(_ddlNumOfWeeks.SelectedValue);

                if (numOfWeeks >= 4 && numOfWeeks <= 11)
                {
                    if (studySchedule == 12)
                        _rate = 150;
                    else if (studySchedule == 15)
                        _rate = 180;
                    else if (studySchedule == 21)
                        _rate = 250;
                    else if (studySchedule == 27)
                        _rate = 300;
                    else if (studySchedule == 30)
                        _rate = 330;
                }
                else if (numOfWeeks >= 12 && numOfWeeks <= 23)
                {
                    if (studySchedule == 12)
                        _rate = 145;
                    else if (studySchedule == 15)
                        _rate = 175;
                    else if (studySchedule == 21)
                        _rate = 240;
                    else if (studySchedule == 27)
                        _rate = 285;
                    else if (studySchedule == 30)
                        _rate = 315;
                }
                else if (numOfWeeks >= 24 && numOfWeeks <= 47)
                {
                    if (studySchedule == 12)
                        _rate = 140;
                    else if (studySchedule == 15)
                        _rate = 170;
                    else if (studySchedule == 21)
                        _rate = 230;
                    else if (studySchedule == 27)
                        _rate = 270;
                    else if (studySchedule == 30)
                        _rate = 300;
                }
                else if (numOfWeeks >= 48)
                {
                    if (studySchedule == 12)
                        _rate = 135;
                    else if (studySchedule == 15)
                        _rate = 165;
                    else if (studySchedule == 21)
                        _rate = 220;
                    else if (studySchedule == 27)
                        _rate = 255;
                    else if (studySchedule == 30)
                        _rate = 285;
                }

                if (_rate != 0)
                {
                    _tuitionFees = _rate * numOfWeeks;
                    _totalFees += _tuitionFees;
                    _homeStayFee = Convert.ToInt32(double.Parse(_ddlAccommodation.SelectedValue)) / 4 * numOfWeeks;
                    _totalFees += _homeStayFee;
                    _totalFees += int.Parse(_ddlAirportTransfer.SelectedValue);
                    _totalFees += _homeStayPlacementFee; //Homestay Placement Fee
                    _medicalFee = 15 * numOfWeeks; // Medical Fee
                    _totalFees += _medicalFee;
                    _totalFees += _registrationFee; // Registration Fee

                    if (studySchedule == 12)
                        materialFees = 2.0;
                    else if (studySchedule == 15)
                        materialFees = 2.25;
                    else if (studySchedule == 21)
                        materialFees = 2.5;
                    else if (studySchedule == 27)
                        materialFees = 3.25;
                    else if (studySchedule == 30)
                        materialFees = 3.5;
                    _materialFee = Convert.ToInt32(materialFees * numOfWeeks);
                    _totalFees += _materialFee;

                    CreateDynamicTable();
                    _pnlTable.Visible = true;
                }
                else
                {
                    _pnlTable.Visible = false;
                    return;
                }

            }
        }

        private void CreateDynamicTable()
        {
            _pnlTable.Controls.Clear();

            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.CssClass = "SummaryTable";
            // Add the table to the placeholder control
            _pnlTable.Controls.Add(tbl);
            // Now iterate through the table and add your controls 

            TableHeaderRow thr = new TableHeaderRow();
            TableHeaderCell thc = new TableHeaderCell();
            thc.Text = "Item";
            thc.Width = 300;                           //add by gary
            thr.Cells.Add(thc);

            thc = new TableHeaderCell();
            thc.Text = "Weeks";
            thc.Width = 130;                           //add by gary
            thr.Cells.Add(thc);
            
            

            thc = new TableHeaderCell();
            thc.Text = "Price";
            thc.Width = 130;                          //add by gary
            thr.Cells.Add(thc);
            
            tbl.Rows.Add(thr);

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Text = "Tuition ($" + _rate + "/week)";
            tc.Width = 300;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = _ddlNumOfWeeks.SelectedValue;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = "$ " + _tuitionFees;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tbl.Rows.Add(tr);

            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Registration Fee";
            tc.Width = 300;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = string.Empty;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = "$ " + _registrationFee;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tbl.Rows.Add(tr);

            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Material Fee";
            tc.Width = 300;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = string.Empty;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = "$ " + _materialFee;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tbl.Rows.Add(tr);

            if (!_ddlAccommodation.SelectedValue.Equals("0"))
            {
                tr = new TableRow();
                tc = new TableCell();
                tc.Text = "Homestay Fee";
                tc.Width = 300;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = _ddlNumOfWeeks.SelectedValue;
                tc.Width = 130;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = "$ " + _homeStayFee;
                tc.Width = 130;
                tr.Cells.Add(tc);

                tbl.Rows.Add(tr);

                tr = new TableRow();
                tc = new TableCell();
                tc.Text = "Homestay Placement Fee";
                tc.Width = 300;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = string.Empty;
                tc.Width = 130;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = "$ " + _homeStayPlacementFee;
                tc.Width = 130;
                tr.Cells.Add(tc);

                tbl.Rows.Add(tr);
            }

            if (!_ddlAirportTransfer.SelectedValue.Equals("0"))
            {
                tr = new TableRow();
                tc = new TableCell();
                tc.Text = "Airport Pick-up Fee - " + _ddlAirportTransfer.SelectedItem.Text;
                tc.Width = 300;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = string.Empty;
                tc.Width = 130;
                tr.Cells.Add(tc);

                tc = new TableCell();
                tc.Text = "$ " + _ddlAirportTransfer.SelectedValue;
                tc.Width = 130;
                tr.Cells.Add(tc);

                tbl.Rows.Add(tr);
            }

            tr = new TableRow();
            tc = new TableCell();
            tc.Text = "Medical Insurance";
            tc.Width = 300;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = _ddlNumOfWeeks.SelectedValue;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tc = new TableCell();
            tc.Text = "$ " + _medicalFee;
            tc.Width = 130;
            tr.Cells.Add(tc);

            tbl.Rows.Add(tr);

            thr = new TableHeaderRow();
            thc = new TableHeaderCell();
            thc.Text = string.Empty;
            tc.Width = 300;
            thr.Cells.Add(thc);

            thc = new TableHeaderCell();
            thc.Text = "Grand Total:";
            tc.Width = 130;
            thr.Cells.Add(thc);

            thc = new TableHeaderCell();
            thc.Text = "$ " + _totalFees;
            tc.Width = 130;
            thr.Cells.Add(thc);

            tbl.Rows.Add(thr);
        }

        private int _tuitionFees;
        private int _registrationFee = 110;
        private int _materialFee;
        private int _homeStayPlacementFee = 210;
        private int _medicalFee;
        private int _rate = 0;
        private int _homeStayFee;
        private int _totalFees = 0;
    }
}